﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Demonstartor
{
    /// <summary>
    /// Class for holding information about fight
    /// </summary>
    class Flight
    {
        public string FlightName { get; private set; }
        public string PlaneName { get; private set; }
        public int ParameterCount { get; private set; }
        public List<string> ParameterNames { get; private set; }
        public List<string> ParameterDescriptions { get; private set; }
        public int FlightSize { get; private set; }
        public TimeSpan TimeOffset { get; private set; }

        public DateTime StartTime;
        public DateTime EndTime;

        private CFmanager CF;
        private CFmanager CFRK = null;

        int WithoutRKCount;

        public int Offset { get; private set; }

        public Flight(string FileName, int Offset, TimeSpan TimeOffset, string RKFileName = null)
        {
            CF = new CFmanager();
            CF.InitDLL(FileName, -1);

            if (RKFileName != null)
            {
                CFRK = new CFmanager();
                CFRK.InitDLL(RKFileName, -1);
            }

            FlightName = CF.GetFlightName();
            PlaneName = CF.GetPlaneName();

            FlightSize = CF.GetFlightSize();

            // Initialization of parameters
            ParameterNames = CF.GetParameterNames();
            WithoutRKCount = ParameterNames.Count;
            if (CFRK != null)
                ParameterNames.AddRange(CFRK.GetParameterNames());

            ParameterDescriptions = CF.GetParameterDescriptions();
            if (CFRK != null)
                ParameterDescriptions.AddRange(CFRK.GetParameterDescriptions());

            ParameterCount = ParameterNames.Count();

            this.TimeOffset = TimeOffset;

            StartTime = DateTime.Parse(CF.GetStartDateTimeXML());
            EndTime = DateTime.Parse(CF.GetStopDateTimeXML());

            this.Offset = Offset;
        }

        public double[][] GetParameters(int[] Inds, int Start, int End)
        {
            double[][] ParameterValues;
            double[][] ParameterValuesRK = null;
            
            unsafe
            {
                int ICount = Inds.Count();
                int* a = stackalloc int[WithoutRKCount];
                int* ark = stackalloc int[ParameterCount - WithoutRKCount];
                for (int i = 0; i < ICount; i++)
                {
                    if (Inds[i] < WithoutRKCount)
                        a[Inds[i]] = 1;
                    else
                        ark[Inds[i] - WithoutRKCount] = 1;
                }

                ParameterValues = CF.GetProcessedValues(Start, End, 4, a, Offset);
                if (CFRK != null)
                    ParameterValuesRK = CFRK.GetProcessedRKValues(Start, End, 4, ark);
            }

            List<double[]> AllParameterValues = new List<double[]>();
            for (int i = 0; i < ParameterValues.Count(); i++)
                AllParameterValues.Add(ParameterValues[i]);
            if (CFRK != null)
            {
                for (int i = 0; i < ParameterValuesRK.Count(); i++)
                    AllParameterValues.Add(ParameterValuesRK[i]);
            }

            return AllParameterValues.ToArray();
        }

        public int DateTimeToFlightInd(DateTime DT)
        {
            return (int)Math.Round((DT - StartTime - TimeOffset).TotalSeconds);
        }



        public Dictionary<string, double[]> GetParameters(string[] Names, DateTime Start, DateTime End)
        {
            int StartIndex = DateTimeToFlightInd(Start);
            int EndIndex = DateTimeToFlightInd(End);

            return GetParameters(Names, StartIndex, EndIndex);
        }

        public Dictionary<string, double[]> GetParameters(string[] Names, int Start, int End)
        {
            List<string> PNames = new List<string>(ParameterNames);
            int PCount = Names.Count();
            int[] Inds = new int[PCount];

            List<string> PN = new List<string>(ParameterNames);

            for (int i = 0; i < PCount; i++)
                Inds[i] = PN.IndexOf(Names[i]);

            double[][] Values = GetParameters(Inds.ToArray(), Start, End);

            Dictionary<string, double[]> Res = new Dictionary<string, double[]>();

            int VCount = Values.Count();

            for (int i = 0; i < VCount; i++)
                Res.Add(Names[i], Values[i]);

            return Res;
        }
    }
}
